//
//  LoginVC.m
//  YaoHeAPP
//
//  Created by mac on 15/11/20.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "LoginVC.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
#import "RegistVC.h"
#import "Tool.h"
#import "Controller.h"
#import "Define.h"
#import "SVProgressHUD.h"
#import "ForgetPasswordVC.h"
#import "ClassData.h"
#import "CityPicker.h"
#import "YunBaService.h"
#import "UMMobClick/MobClick.h"


#import "RegistViewController.h"

#define iPhone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)


@interface LoginVC ()

@end

@implementation LoginVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"LoginVC"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"LoginVC"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    width=self.view.frame.size.width;
    height=self.view.frame.size.height;
    imageArray=[NSArray arrayWithObjects:@"home",@"pressed",@"Release_for",@"order",@"myself",nil];
    simageArray=[NSArray arrayWithObjects:@"homes",@"presseds",@"Release_for_hl",@"orders",@"myselfs",nil];
    
    //设置登录界面的整体背景
    UIImageView*imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
        imageView.backgroundColor=[UIColor greenColor];
    imageView.image=[UIImage imageNamed:@"background.jpg"];
    [self.view addSubview:imageView];
    
    
    //背景
    UIView*backView=[[UIView alloc]init];
    UIColor*white=[UIColor whiteColor];
    UIColor*backColor=[white colorWithAlphaComponent:0.2];
    backView.backgroundColor=backColor;
    [self.view addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(height/2-88);
        make.left.offset(20);
        make.right.offset(-20);
        make.height.offset(90);
    }];
    
    
    UILabel *TIT=[[UILabel alloc]init];
    TIT.frame=CGRectMake(0, 25, self.view.frame.size.width, 50);
    TIT.font=[UIFont systemFontOfSize:12];
    TIT.textAlignment=NSTextAlignmentLeft;
    TIT.numberOfLines=0;
    TIT.textColor=[UIColor whiteColor];
    TIT.backgroundColor=[UIColor colorWithRed:251.f/255.f green:87.f/255.f blue:60.f/255.f alpha:1];
    TIT.text=@"信用行账号和吆喝账号合并了，如果您的手机号在信用行和吆喝都注册了，请使用“信用行登录密码”登陆。给您带来不便请见谅！";
    [self.view addSubview:TIT];
    //    [self.view bringSubviewToFront:TIT];
    TIT.layer.zPosition = 10;
    
    
    //用户头像
    UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"ic_logo_login"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"ic_logo_login"] forState:UIControlStateHighlighted];
    [self.view addSubview:button];
    
    if (iPhone4) {
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(backView.mas_top).offset(-36);
            make.left.offset(width/2-width/8);
            make.height.offset(width/4/1.129);
            make.width.offset(width/4);
        }];
        
    }else{
    
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(backView.mas_top).offset(-36-30);
            make.left.offset(width/2-width/8);
            make.height.offset(width/4/1.129);
            make.width.offset(width/4);
        }];
    }
    //手机图片
    UIImageView*phoneImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"phone"]];
    [backView addSubview:phoneImage];
    [phoneImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(7);
        make.top.offset(7);
        make.height.offset(30);
        make.width.offset(30);
    }];
    
    //请输入手机号的输入框
    numberPhone=[[UITextField alloc]init];
    numberPhone.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
    numberPhone.borderStyle=UITextBorderStyleNone;
    numberPhone.clearButtonMode=UITextFieldViewModeWhileEditing;
    numberPhone.placeholder=@"请输入手机号";
    [numberPhone setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    numberPhone.delegate=self;
    [numberPhone addTarget:self action:@selector(disMiss) forControlEvents:UIControlEventEditingDidEndOnExit];
    [backView addSubview:numberPhone];
    [numberPhone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(44);
        make.right.offset(-6);
        make.top.offset(7);
        make.bottom.offset(-51);
    }];
    
    UIImageView*lineImg=[[UIImageView alloc]init];
    lineImg.backgroundColor=[UIColor colorWithRed:25.f/255.f green:132.f/255.f blue:137.f/255.f alpha:1];
    [backView addSubview:lineImg];
    [lineImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.offset(0),   make.top.equalTo(backView.mas_centerY).offset(0.5);
        make.right.offset(0),  make.height.offset(1);
    }];
    
    
    
    //锁图片
    UIImageView*lockImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"lock"]];
    [backView addSubview:lockImage];
    [lockImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(51);
        make.left.offset(7);
        make.height.offset(30);
        make.width.offset(30);
    }];
    
    //密码输入框
    password=[[UITextField alloc]init];
    password.borderStyle=UITextBorderStyleNone;
    password.placeholder=@"请输入6~20位字母或数字";
    [password setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    password.clearButtonMode=UITextFieldViewModeWhileEditing;
    password.secureTextEntry=YES;
    password.delegate=self;
    [password addTarget:self action:@selector(disMiss) forControlEvents:UIControlEventEditingDidEndOnExit];
    [backView addSubview:password];
    [password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(44);
        make.right.offset(-7);
        make.top.offset(51);
        make.bottom.offset(-7);
    }];

    //登录按钮
    UIButton*loginBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    UIImage*loginOldImg=[UIImage imageNamed:@"loginbutton"];
    UIImage*loginNewImg=[loginOldImg stretchableImageWithLeftCapWidth:15 topCapHeight:0];
    [loginBtn setBackgroundImage:loginNewImg forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backView.mas_bottom).offset(20);
        make.left.offset(33);
        make.right.offset(-33);
        make.height.offset(40);
    }];
    
    //注册按钮
    UIButton*registBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [registBtn setTitle:@"还没有账户?" forState:UIControlStateNormal];
    
    registBtn.layer.masksToBounds=YES;
    registBtn.layer.cornerRadius=18;
    registBtn.layer.borderWidth=1;
    registBtn.layer.borderColor=[UIColor colorWithRed:255.f/255.f green:255.f/255.f blue:255.f/255.f alpha:1].CGColor;
    registBtn.titleLabel.font=font15;
    
    
    [registBtn addTarget:self action:@selector(regist) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registBtn];
    [registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loginBtn.mas_bottom).offset(10);
        make.left.offset(33);
        make.right.offset(-33);
        make.height.offset(40);
    }];
 
    //忘记密码按钮
    UIButton*forgetBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [forgetBtn setTitle:@"忘记密码？" forState:UIControlStateNormal];
    forgetBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [forgetBtn addTarget:self action:@selector(forgetPassword) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetBtn];
    [forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-45);
        make.left.offset(width/2-44);
        make.width.offset(88);
        make.height.offset(22);
    }];
}

//登录按钮
-(void)login {
    [self.view endEditing:YES];
    if (numberPhone.text.length!=11)
    {
        ALERTSHOW(@"你输入的账号格式不对");
        return;
    }
    if (password.text.length<6||password.text.length>20)
    {
        ALERTSHOW(@"密码格式不正确");
        return;
    }

    NSDictionary*dic=@{@"MOBILE":numberPhone.text,@"PASSWORD":password.text};
    [Controller requestWithURL:LOGINAPI jsondic:dic finishBlock:^(ClassData* data) {
        if ([data.number  integerValue]==0)
        {
            NSString *userid=[NSString stringWithFormat:@"%@",data.data[@"USER_ID"]];
            
            NSString *publisher=[NSString stringWithFormat:@"%@",data.data[@"SESSION_ID"]];
            
            NSString *regtype=[NSString stringWithFormat:@"%@",data.data[@"REGTYPE"]];
            
            NSString *remobile=[NSString stringWithFormat:@"%@",data.data[@"REGMOBILE"]];
            
            [[NSUserDefaults standardUserDefaults] setObject:remobile forKey:kREGMOBILE];
            
            [[NSUserDefaults standardUserDefaults] setObject:regtype forKey:ISREGTYPE];

            [[NSUserDefaults standardUserDefaults] setObject:userid forKey:USERID];
            [[NSUserDefaults standardUserDefaults] setObject:publisher forKey:PUBLISHER];
            
            [YunBaService subscribe:userid resultBlock:^(BOOL succ, NSError *error){
                if (succ) {
                    
                } else {
                    
                }
            }];
            
            [[NSUserDefaults standardUserDefaults]setObject:@"成功" forKey:@"状态"];
            RootTabBarViewController *rootTabBarViewController = [RootTabBarViewController rootViewController];
            [rootTabBarViewController changeRootViewControllerTo:0];
            
            // 友盟登录
            [MobClick profileSignInWithPUID:userid];
        }
        else
        {
            UIAlertView*alertView=[[UIAlertView alloc]initWithTitle:@"提示" message: data.message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alertView show];
        }
    }];
    
}

//注册
-(void)regist
{
    [MobClick event:@"noAccountYet"];
    //进入注册界面
//    RegistVC*registVC=[RegistVC new];
//    [self.navigationController pushViewController:registVC animated:YES];
    
    RegistViewController *registViewController=[[RegistViewController alloc]init];
    [self.navigationController pushViewController:registViewController animated:NO];
    
}

//忘记密码
-(void)forgetPassword
{
    //进入找回密码页面
    ForgetPasswordVC*forgetVC=[ForgetPasswordVC new];
    [self.navigationController pushViewController:forgetVC animated:YES];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.25 animations:^{
        self.view.frame=CGRectMake(0, -50, self.view.frame.size.width, self.view.frame.size.height);
    }];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.25 animations:^{
    self.view.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }];
}

//键盘弹下
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)disMiss
{
    return;
}

@end
